home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / failure.py < prev    next >
Encoding:
Python Source  |  2006-04-14  |  6.5 KB  |  307 lines

  1. # Copyright (c) 2004 Divmod.
  2. # See LICENSE for details.
  3.  
  4. #  failure.py
  5.  
  6. import types
  7. import linecache
  8. import re
  9.  
  10. from nevow import tags as t
  11. from twisted.python import failure
  12.  
  13.  
  14. stylesheet = """
  15. p.error {
  16.   color: black;
  17.   font-family: Verdana, Arial, helvetica, sans-serif;
  18.   font-weight: bold;
  19.   font-size: large;
  20.   margin: 0.25em;
  21. }
  22.  
  23. div {
  24.   font-family: Verdana, Arial, helvetica, sans-serif;
  25. }
  26.  
  27. strong.variableClass {
  28.   font-size: small;
  29. }
  30.  
  31. div.stackTrace {
  32. }
  33.  
  34. div.frame {
  35.   padding: 0.25em;
  36.   background: white;
  37.   border-bottom: thin black dotted;
  38. }
  39.  
  40. div.firstFrame {
  41.   padding: 0.25em;
  42.   background: white;
  43.   border-top: thin black dotted;
  44.   border-bottom: thin black dotted;
  45. }
  46.  
  47. div.location {
  48.     font-size: small;
  49. }
  50.  
  51. div.snippet {
  52.   background: #FFFFDD;
  53.   padding: 0.25em;
  54. }
  55.  
  56. div.snippetHighlightLine {
  57.   color: red;
  58. }
  59.  
  60. span.lineno {
  61.     font-size: small;
  62. }
  63.  
  64. pre.code {
  65.   margin: 0px;
  66.   padding: 0px;
  67.   display: inline;
  68.   font-size: small;
  69.   font-family: "Courier New", courier, monotype;
  70. }
  71.  
  72. span.function {
  73.   font-weight: bold;
  74.   font-family: "Courier New", courier, monotype;
  75. }
  76.  
  77. table.variables {
  78.   border-collapse: collapse;
  79.   width: 100%;
  80. }
  81.  
  82. td.varName {
  83.   width: 1in;
  84.   vertical-align: top;
  85.   font-style: italic;
  86.   font-size: small;
  87.   padding-right: 0.25em;
  88. }
  89.  
  90. td.varValue {
  91.   padding-left: 0.25em;
  92.   padding-right: 0.25em;
  93.   font-size: small;
  94. }
  95.  
  96. div.variables {
  97.   margin-top: 0.5em;
  98. }
  99.  
  100. div.dict {
  101.   background: #cccc99;
  102.   padding: 2px;
  103.   float: left;
  104. }
  105.  
  106. td.dictKey {
  107.   background: #ffff99;
  108.   font-weight: bold;
  109. }
  110.  
  111. td.dictValue {
  112.   background: #ffff99;
  113. }
  114.  
  115. div.list {
  116.   background: #7777cc;
  117.   padding: 2px;
  118.   float: left;
  119. }
  120.  
  121. div.listItem {
  122.   background: #9999ff;
  123. }
  124.  
  125. div.instance {
  126.   width: 100%;
  127.   background: #efefef;
  128.   padding: 2px;
  129.   float: left;
  130. }
  131.  
  132. span.instanceName {
  133.   font-size: small;
  134.   display: block;
  135. }
  136.  
  137. span.instanceRepr {
  138.   font-family: "Courier New", courier, monotype;
  139. }
  140.  
  141. div.function {
  142.   background: orange;
  143.   font-weight: bold;
  144.   float: left;
  145. }
  146. """
  147.  
  148.  
  149. def saferepr(x):
  150.     try:
  151.         rx = repr(x)
  152.     except:
  153.         rx = "repr failed! %s instance at 0x%x" % (x.__class__, id(x))
  154.     return rx
  155.  
  156.  
  157. def htmlDict(d):
  158.     return t.div(_class="dict")[
  159.         t.span(_class="heading")[
  160.             "Dictionary instance @ 0x%x" % id(d)
  161.         ],
  162.         t.table(_class="dict")[[
  163.             t.tr[
  164.                 t.td(_class="dictKey")[ k == '__builtins__' and 'builtin dictionary' or htmlrepr(k) ],
  165.                 t.td(_class="dictValue")[ htmlrepr(v) ]
  166.             ]
  167.             for k, v in d.items()
  168.         ]]
  169.     ]
  170.                 
  171.  
  172. def htmlList(l):
  173.     return t.div(_class="list")[
  174.         t.span(_class="heading")[ "List instance @ 0x%x" % id(l) ],
  175.         [t.div(_class="listItem")[ htmlrepr(i) ] for i in l]
  176.     ]
  177.  
  178.  
  179. def htmlInst(i):
  180.     return t.div(_class="instance")[
  181.         t.span(_class="instanceName")[ "%s instance at 0x%x" % (i.__class__, id(i)) ],
  182.         t.span(_class="instanceRepr")[ saferepr(i) ]
  183.     ]
  184.  
  185.  
  186. def htmlString(s):
  187.     return s
  188.  
  189.  
  190. def htmlFunc(f):
  191.     return t.div(_class="function")[
  192.         "Function %s in file %s at line %s" % (f.__name__, f.func_code.co_filename, f.func_code.co_firstlineno)
  193.     ]
  194.  
  195.  
  196. def htmlMeth(m):
  197.     return t.div(_class="method")[
  198.         "Method %s in file %s at line %s" % (m.im_func.__name__, m.im_func.func_code.co_filename, m.im_func.func_code.co_firstlineno)
  199.     ]
  200.  
  201. def htmlUnknown(u):
  202.     return t.pre[
  203.         saferepr(u)
  204.     ]
  205.  
  206.  
  207. htmlReprTypes = {
  208.     types.DictType: htmlDict,
  209.     types.ListType: htmlList,
  210.     types.InstanceType: htmlInst,
  211.     types.StringType: htmlString,
  212.     types.FunctionType: htmlFunc,
  213.     types.MethodType: htmlMeth,
  214. }
  215.  
  216.  
  217. def htmlrepr(x):
  218.     return htmlReprTypes.get(type(x), htmlUnknown)(x)
  219.  
  220.  
  221. def varTable(usedVars):
  222.     return t.table(_class="variables")[[
  223.         t.tr(_class="varRow")[
  224.             t.td(_class="varName")[ key ],
  225.             t.td(_class="varValue")[ htmlrepr(value) ]
  226.         ]
  227.         for (key, value) in usedVars
  228.     ]]
  229.  
  230.  
  231. def formatFailure(myFailure):
  232.     if not isinstance(myFailure, failure.Failure):
  233.         return t.pre[ str(myFailure) ]
  234.  
  235.     stackTrace = t.div(_class="stackTrace")
  236.     failureOverview = t.p(_class="error")[ str(myFailure.type), ": ", str(myFailure.value) ]
  237.  
  238.     result = [
  239.         t.style(type="text/css")[
  240.             stylesheet,
  241.         ],
  242.         t.a(href="#tracebackEnd")[ failureOverview ],
  243.         stackTrace,
  244.         t.a(name="tracebackEnd")[ failureOverview ]
  245.     ]
  246.  
  247.     first = 1
  248.     for method, filename, lineno, localVars, globalVars in myFailure.frames:
  249.         # It's better to have a line number than nothing at all.
  250.         #if filename == '<string>':
  251.         #    continue
  252.         if first:
  253.             frame = t.div(_class="firstFrame")
  254.             first = 0
  255.         else:
  256.             frame = t.div(_class="frame")
  257.         stackTrace[ frame ]
  258.  
  259.         snippet = t.div(_class="snippet")
  260.         frame[
  261.             t.div(_class="location")[
  262.                 filename, ", line ", lineno, " in ", t.span(_class="function")[ method ]
  263.             ],
  264.             snippet,
  265.         ]
  266.  
  267.         textSnippet = ''
  268.         for snipLineNo in range(lineno-2, lineno+2):
  269.             snipLine = linecache.getline(filename, snipLineNo)
  270.             textSnippet += snipLine
  271.             if snipLineNo == lineno:
  272.                 snippetClass = "snippetHighlightLine"
  273.             else:
  274.                 snippetClass = "snippetLine"
  275.             snippet[
  276.                 t.div(_class=snippetClass)[
  277.                     t.span(_class="lineno")[ snipLineNo ],
  278.                     t.pre(_class="code")[ snipLine ]
  279.                 ]
  280.             ]
  281.  
  282.         # Instance variables
  283.         for name, var in localVars:
  284.             if name == 'self' and hasattr(var, '__dict__'):
  285.                 usedVars = [ (key, value) for (key, value) in var.__dict__.items()
  286.                              if re.search(r'\Wself.%s\W' % (re.escape(key),), textSnippet) ]
  287.                 if usedVars:
  288.                     frame[
  289.                         t.div(_class="variables")[
  290.                             t.strong(_class="variableClass")[ "Self" ],
  291.                             varTable(usedVars)
  292.                         ]
  293.                     ]
  294.                     break
  295.  
  296.         # Local and global vars
  297.         for nm, varList in ('Locals', localVars), ('Globals', globalVars):
  298.             usedVars = [ (name, var) for (name, var) in varList
  299.                          if re.search(r'\W%s\W' % (re.escape(name),), textSnippet) ]
  300.             if usedVars:
  301.                 frame[
  302.                     t.div(_class="variables")[ t.strong(_class="variableClass")[ nm ] ],
  303.                     varTable(usedVars)
  304.                 ]
  305.  
  306.     return result
  307.